﻿package kaze.controllers.models.menu 
{
    import lunas.Button;
    
    import ninjas.ninpo.display.ListValueObject;
    import ninjas.ninpo.vo.MenuVO;
    
    import system.events.Command;
    
    import vegas.controllers.AbstractController;
    import vegas.events.ModelObjectEvent;
    import vegas.ioc.factory.ObjectFactory;
    
    import flash.events.Event;
    /**
     * Invoked when the menu model change the current MenuVO.
     */
    public class ChangeMenu extends AbstractController 
    {
        /**
         * Creates a new ChangeMenu instance.
         */
        public function ChangeMenu()
        {
            super();
        }
        
        /**
         * The IoC container reference.
         */
        public var factory:ObjectFactory ;
        
        /**
         * Handles the event.
         */
        public override function handleEvent(e : Event):void 
        {
            var vo:MenuVO = (e as ModelObjectEvent).getVO() as MenuVO ;
            if ( vo != null )
            {
                logger.debug( this + " handleEvent : " + vo ) ;
                
                ////////////// force the cell of the current menu value object to be selected
                
                if( factory && factory.containsObject( vo.type ) )
                {
                    var list:ListValueObject = factory.getObject( vo.type ) as ListValueObject ;
                    if ( list )
                    {
                        var cell:Button = list.getCell(vo) as Button ;
                        if( cell && cell.toggle && cell.groupName )
                        {
                            cell.setSelected( true, true ) ;
                        }
                    }
                }
                
                ////////////// run the command of the current menu
                
                var command:Command = vo.command ;
                if ( command != null )
                {
                    command.run() ; 
                }
                else
                {
                    logger.warn( this + " failed with a 'null' or 'undefined' Command object in the MenuVO : " + vo ) ; 
                }
            }
            else
            {
                logger.warn( this + " failed with a 'null' or 'undefined' ValueObject." ) ;
            }
        }
    }
}
